 package com.zu.commu.auto1_0.protocol.initValue;



   import com.zu.commu.auto1_0.DataType1_0;
   import com.zu.commu.auto1_0.protocol.CheckData;
   import com.zu.commu.auto1_0.protocol.Util;
   import com.zu.commu.exception.CommuException;

 public class SetInitValueProtocol
 {
   public byte[] remoteData(String meterNo, String value)
     throws Exception
   {
     String temp = "";
     try {
       temp = checkValue(value);
       temp = canonValue(temp);
     } catch (CommuException e) {
       throw e;
     }

     byte[] buf = new byte[18];
     buf[0] = 104;
     buf[1] = 12;
     buf[2] = 12;
     buf[3] = 104;
     Util u = new Util();
     try {
       byte[] b = u.hex2Byte(meterNo);
       for (int i = 0; i < b.length; i++)
         buf[(4 + i)] = b[i];
     }
     catch (Exception e) {
       throw new CommuException().setMessage(
         "测控器地址号不合法！" + e.getMessage(), e);
     }
     buf[10] = 64;
     buf[11] = (byte)DataType1_0.INITVALUE.toInt();
     try
     {
       byte[] b = u.hex2Byte(temp);
       for (int i = 0; i < b.length; i++)
         buf[(12 + i)] = b[i];
     }
     catch (Exception e) {
       throw new CommuException().setMessage("表底值不合法值！" + e.getMessage(),
         e);
     }

     buf[16] = new CheckData().getCrcCheck(buf, 4, 15);
     buf[17] = 22;
     return buf;
   }

   public String checkValue(String value)
     throws CommuException
   {
     value = new Util().cleanDotNumber(value);
     if (value.equals("")) {
       throw new CommuException().setMessage("表底值不合法值！", null);
     }
     if (value.startsWith("-")) {
       throw new CommuException().setMessage("表底值不合法值！", null);
     }
     int temp = 0;
     try {
       temp = Integer.parseInt(value);
     } catch (Exception e) {
       throw new CommuException().setMessage("表底值不合法值！", null);
     }
     if (temp > 9999999) {
       throw new CommuException().setMessage("表底值超出合法值范围！", null);
     }
     return temp+"";
   }

   public String canonValue(String value)
   {
     String temp = value;
     if (temp.length() < 8) {
       int n = 8 - temp.length();
       for (int i = 0; i < n; i++) {
         temp = "0" + temp;
       }
     }
     return temp;
   }

   public static void main(String[] args)
   {
     String s = "999";
     SetInitValueProtocol d = new SetInitValueProtocol();
     s = d.canonValue(s);
     System.out.print(s);
   }
 }
